package com.maoslv.service.staff;

import cn.hutool.core.map.MapBuilder;
import com.maoslv.mapper.StaffMapper;
import com.maoslv.pojo.Pagination;
import com.maoslv.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    StaffMapper staffMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<Object,Object> getStaffList(Pagination pagination) {
        pagination.setCurrentPage((pagination.getCurrentPage() - 1) * pagination.getPageSize());
        Integer total = staffMapper.getTotal(pagination);
        List<Staff> staffList = staffMapper.getStaffList(pagination);
        return MapBuilder.create().
                put("staffList", staffList)
                .put("total", total)
                .map();
    }

    @Override
    public List<String> getDepartmentList() {
        return staffMapper.getDepartmentList();
    }

    @Override
    public Staff getStaffInfo(Integer id) {
        return staffMapper.getStaffInfo(id);
    }

    @Override
    public int updateStaff(Staff staff) {
        return staffMapper.updateStaff(staff);
    }

    @Override
    public int addStaff(Staff staff) {
        staff.setEntryTime(new Date());
        return staffMapper.addStaff(staff);
    }
}
